package dgo.interval;
import dgo.Callback;

public class Lerp extends Interval{
  private int _fromValue, _toValue;
  private Callback _callback;
  private Object _argument;
  
  public int getDuration(){return this.duration;}
  
  public Lerp(int duration, int fromValue, int toValue, Callback callback){
    this.duration = duration;
    _callback = callback;
    _argument = null;
    _fromValue = fromValue;
    _toValue = toValue;
  }
  
  public Lerp(int duration, int fromValue, int toValue, Object argument, Callback callback){
    this.duration = duration;
    _callback = callback;
    _argument = argument;
    _fromValue = fromValue;
    _toValue = toValue;
  }
  
  private int value(int counter){
    return (_toValue * counter + _fromValue * (this.duration - counter)) / duration;
  }
  
  public boolean call(IntervalNode node){
    if(this.duration == 0) return false;
    
    int d;
    if(this.duration == -1){
      d = node.parent.duration;
    }else{
      d = this.duration;
    }

    if(node.counter < 0 || node.counter >= d) return false;
    node.counter++;
    if(_callback != null) _callback.call(this.value(node.counter), _argument);
    
    return true;
  }
}
